﻿namespace Brock.Wpf.Controls
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;

    using Microsoft.Win32;

    /// <summary>
    /// Interaction logic for FileChooser.xaml
    /// </summary>
    public partial class FileChooser : UserControl
    {
        public FileChooser()
        {
            this.InitializeComponent();
        }

        public string FileNameLabelText
        {
            set
            {
                this.fileNameLabel.Content = value;
            }
        }

        public string SelectedPath
        {
            get
            {
                return this.pathTextBox.Text;
            }

            set
            {
                this.pathTextBox.Text = value;
            }
        }

        protected FileDialog CreateFileDialog()
        {
            switch (FileChooserMode)
            {
                case FileChooserMode.Open:
                    return new OpenFileDialog();
                case FileChooserMode.Save:
                    return new SaveFileDialog();
                default:
                    Debug.Assert(false, "has a new FileChooserMode been added?");
                    return null;
            }
        }

        private void OnBrowseButtonClick(object sender, RoutedEventArgs e)
        {
            FileDialog dialog = this.CreateFileDialog();
            if (dialog == null)
            {
                return;
            }

            if (File.Exists(this.SelectedPath))
            {
                dialog.FileName = this.SelectedPath;
            }

            dialog.Filter = "XML Files (*.xml)|*.xml";
            bool? result = dialog.ShowDialog();
            if (result == true)
            {
                this.SelectedPath = dialog.FileName;
            }
        }

        public enum FileChooserMode
        {
            Open,
            Save
        }

    }
}
